<template>
  <div class="register-container">
    <div class="register-form">
      <h2>教师注册</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="formData.username"
            required
            placeholder="请输入用户名"
          />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="formData.email"
            required
            placeholder="请输入邮箱"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="formData.password"
            required
            placeholder="请输入密码"
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="formData.confirmPassword"
            required
            placeholder="请再次输入密码"
          />
        </div>
        
        <div class="form-group">
          <label for="fullName">姓名</label>
          <input
            type="text"
            id="fullName"
            v-model="formData.fullName"
            required
            placeholder="请输入真实姓名"
          />
        </div>
        
        <div class="form-group">
          <label for="phone">电话</label>
          <input
            type="tel"
            id="phone"
            v-model="formData.phone"
            placeholder="请输入电话号码"
          />
        </div>
        
        <div class="form-group">
          <label for="department">部门</label>
          <input
            type="text"
            id="department"
            v-model="formData.department"
            placeholder="请输入所属部门"
          />
        </div>
        
        <div class="form-actions">
          <button type="submit" :disabled="loading">
            {{ loading ? '注册中...' : '注册' }}
          </button>
          <button type="button" @click="$router.push('/login')" class="btn-secondary">
            返回登录
          </button>
        </div>
        
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
        
        <div v-if="success" class="success-message">
          注册成功！请登录
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import api from '../api'

export default {
  name: 'Register',
  data() {
    return {
      formData: {
        username: '',
        email: '',
        password: '',
        confirmPassword: '',
        fullName: '',
        phone: '',
        department: '',
        role: 'teacher'
      },
      loading: false,
      error: '',
      success: false
    }
  },
  methods: {
    async handleRegister() {
      // 验证密码
      if (this.formData.password !== this.formData.confirmPassword) {
        this.error = '两次输入的密码不一致'
        return
      }
      
      // 验证密码长度
      if (this.formData.password.length < 6) {
        this.error = '密码长度至少6位'
        return
      }
      
      this.loading = true
      this.error = ''
      this.success = false
      
      try {
        const registerData = {
          username: this.formData.username,
          email: this.formData.email,
          password: this.formData.password,
          full_name: this.formData.fullName,
          phone: this.formData.phone,
          department: this.formData.department,
          role: this.formData.role
        }
        
        const response = await api.auth.register(registerData)
        
        if (response.data) {
          this.success = true
          setTimeout(() => {
            this.$router.push('/login')
          }, 2000)
        }
      } catch (error) {
        console.error('Registration error:', error)
        if (error.response?.data?.detail) {
          this.error = error.response.data.detail
        } else {
          this.error = '注册失败，请稍后重试'
        }
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.register-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #555;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

button {
  flex: 1;
  padding: 0.75rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

button[type="submit"] {
  background-color: #4CAF50;
  color: white;
}

button[type="submit"]:hover:not(:disabled) {
  background-color: #45a049;
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background-color: #f44336;
  color: white;
}

.btn-secondary:hover {
  background-color: #da190b;
}

.error-message {
  color: #f44336;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: #ffebee;
  border-radius: 4px;
}

.success-message {
  color: #4CAF50;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: #e8f5e8;
  border-radius: 4px;
}
</style>